package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.LikesBehaviorDto;
import com.heima.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadLocal.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 存储喜欢数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //1.参数效验
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "该文章不存在");
        }

        //2.判断是否是登录用户还是游客身份
        ApUser apUser = ApUserThreadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = apUser.getId();
        if (userId == 0) {
            //3.如果是游客则终止程序
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "游客身份,没有权限执行点赞");
        }

        //4.判断当前用户是否点赞过该文章
        String likeKey = "USER_LIKES_" + dto.getArticleId();
        Boolean flag = redisTemplate.opsForHash().hasKey(likeKey, userId.toString());

        //5.如果点赞过,且正在执行的是点赞,则终止
        if (flag && dto.getOperation() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "当前用户点赞过该文章");
        }

        //6.如果点赞过,且正在执行的是取消点赞,则删除redis和mongo
        if (flag && dto.getOperation() == 1) {
            redisTemplate.opsForHash().delete(likeKey, userId.toString());


            mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("articeId").is(dto.getArticleId())), ApLikesBehavior.class);
        }

        //7.如果没有点赞过,且正在执行的是点赞,则新增redis和mongo
        if (!flag && dto.getOperation() == 0) {
            redisTemplate.opsForHash().put(likeKey, userId.toString(), "1");

            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setUserId(Long.valueOf(userId));
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setType((short) 0);
            mongoTemplate.save(apLikesBehavior);
            //8.如果没有点赞过,且正在执行的是取消点赞,则终止
            if (flag && dto.getOperation() == 1) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "当前用户已经取消点赞了");
            }
        }
            //9.返回数据
        return ResponseResult.okResult("点赞|取消点赞完成");
    }
}